package com.network.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.network.service.common.ApiResultGenerator;
import com.network.service.common.BaseResult;
import com.network.service.common.ConvertMapUtils;
import com.network.service.entity.User;
import com.network.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "用户接口")
public class UserController {

    @Autowired
    private UserService userService;


    @ApiOperation(value = "通过uuid获取用户信息")
    @RequestMapping(value = "user/getUserByUuid",method = {RequestMethod.GET,RequestMethod.DELETE,RequestMethod.HEAD,RequestMethod.PATCH,RequestMethod.OPTIONS})
    public BaseResult getUserByUuid(@RequestParam(value = "userUuid", required = true)
                                    @ApiParam(name = "userUuid", value = "调用参数是用户的uuid值", required = true) String userUuid,
                                    @RequestHeader(value = "Accept-Language", required = false)
                                    @ApiParam(name = "Accept-Language", value = "客户端支持的语言", defaultValue = "zh-CN", required = false)
                                    String acceptLanguage) throws Exception {
        if (StringUtils.isNoneBlank(userUuid)) {
            QueryWrapper<User> entityWrapper = new QueryWrapper<>();
            entityWrapper.eq("user_uuid", userUuid);
            List<User> shopUserList = userService.list(entityWrapper);
            if (shopUserList.size() == 1) {
                User user = shopUserList.get(0);
                if ("zh-CN".equals(acceptLanguage)) {
                    user.setName("夏耿文");
                }
                Map resultMap = ConvertMapUtils.getUserMap(user);
                return ApiResultGenerator.successResult(resultMap);
            }
        }
        throw new Exception("error getUserByUuid");
    }


    @ApiOperation(value = "创建新的用户信息")
    @RequestMapping(value = "user/createUser",method = {RequestMethod.POST,RequestMethod.PUT})
    public boolean createUser(@ApiParam(name = "user", value = "插入新用户", required = true)
                              @RequestBody User user) {
        return userService.saveOrUpdate(user);
    }
}
